package cn.michael.dev.leetcode;

/**
 * Created by hufenggang on 2021/11/3.
 */
public class Problem88 {

    /**
     * 合并两个有序数组
     *
     * @param nums1
     * @param m
     * @param nums2
     * @param n
     */
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int p1 = 0, p2 = 0;
        int[] newNums = new int[m + n];
        int curr;
        while(p1 < m || p2 < n) {
            if (p1 == m) {
                curr = nums2[p2++];
            } else if (p2 == n) {
                curr = nums2[p1++];
            } else if (nums1[p1] < nums2[p2]) {
                curr = nums1[p1++];
            } else {
                curr = nums2[p2++];
            }

            newNums[p1 + p2 - 1] = curr;
        }

        for (int i = 0; i < m + n; ++i) {
            nums1[i] = newNums[i];
        }
    }
}
